package com.hsd.whb.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hsd.whb.converter.MeetingParticipateTextConverter;
import com.hsd.whb.entity.Meeting;
import com.hsd.whb.entity.MeetingParticipateText;
import com.hsd.whb.enums.MeetingStatusEnum;
import com.hsd.whb.enums.ResultEnum;
import com.hsd.whb.exception.HwbException;
import com.hsd.whb.pojo.output.MeetingParticipateTextVO;
import com.hsd.whb.repository.MeetingParticipateTextRepository;
import com.hsd.whb.repository.MeetingRepository;
import com.hsd.whb.service.MeetingParticipateTextService;
import com.hsd.whb.service.MeetingService;
import com.hsd.whb.service.ParticipantsService;
import com.hsd.whb.utils.DateUtil;
import com.wangjiacai.hqlQuery.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class MeetingParticipateTextServiceImpl extends BaseServiceImpl implements MeetingParticipateTextService {

    @Autowired
    private MeetingService meetingService;

    @Autowired
    private ParticipantsService participantsService;

    @Autowired
    private MeetingParticipateTextRepository repository;

    @Autowired
    private MeetingRepository meetingRepository;

    @Override
    public MeetingParticipateText addMeetingText(String text, Integer meetingId, Date startTime, Date endTime, Integer participateId) {
        validMeetingParticipateText(meetingId);
        MeetingParticipateText meetingParticipateText = new MeetingParticipateText();
        meetingParticipateText.setDeleted(0);
        meetingParticipateText.setMeetingId(meetingId);
        meetingParticipateText.setParticipantsId(participateId);
        meetingParticipateText.setText(text);
        meetingParticipateText.setStartTime(startTime);
        meetingParticipateText.setEndTime(endTime);
        return repository.save(meetingParticipateText);
    }

    @Override
    public List<MeetingParticipateTextVO> getMeetingTextByMeetingId(Integer meetingId) {
        List<MeetingParticipateText> list = repository.findByMeetingIdOrderByStartTimeAsc(meetingId);
        return MeetingParticipateTextConverter.convertToMeetingParticipateTextVOList(list, participantsService);
    }

    private void validMeetingParticipateText(Integer meetingId) {
        Meeting meeting = meetingService.findMeetingById(meetingId);
        if (meeting == null) throw new HwbException(ResultEnum.MEETING_NOT_EXIST);
        if (meeting.getMeetingStatus() != MeetingStatusEnum.PROGRESSING.getCode())
            throw new HwbException(ResultEnum.MEETING_STATUS_ERROR);
    }

    @Override
    public JSONObject getMeetingFileDataByMeetingId(Integer meetingId, Integer intervalSeconds) {
        JSONObject jsonObject = new JSONObject();
        Meeting meeting = meetingRepository.getOne(meetingId);
        List<MeetingParticipateTextVO> list = getMeetingTextByMeetingId(meetingId);

        jsonObject.put("title", meeting.getTitle());
        jsonObject.put("startTime", DateUtil.format(meeting.getStartTime(), "yyyy-MM-dd HH:mm:ss"));
        JSONArray personList = new JSONArray();
        JSONObject person = null;
        JSONArray textList = null;
        String textString = "";
        for (int i = 0; i < list.size(); i++) {
            if (i != 0) {
                if (list.get(i).getParticipantsName().equals(list.get(i - 1).getParticipantsName())) {
                    LocalDateTime prev = DateUtil.dateToLocalDateTime(list.get(i - 1).getEndTime());
                    LocalDateTime next = DateUtil.dateToLocalDateTime(list.get(i).getStartTime());
                    if (prev.plusSeconds(intervalSeconds).isBefore(next)) {
                        textList.add(textString);
                        textString = list.get(i).getText();
                    } else {
                        textString += list.get(i).getText();
                    }
                } else {
                    textList.add(textString);
                    person.put("textList", textList);
                    personList.add(person);

                    person = new JSONObject();
                    person.put("participantsName", list.get(i).getParticipantsName());
                    textList = new JSONArray();
                    textString = list.get(i).getText();
                }
            } else {
                person = new JSONObject();
                person.put("participantsName", list.get(i).getParticipantsName());

                textList = new JSONArray();
                textString = list.get(i).getText();
            }
            if (i == list.size() - 1) {
                textList.add(textString);
                person.put("textList", textList);
                personList.add(person);
            }
        }
        jsonObject.put("personList", personList);
        return jsonObject;
    }
}
